roi
A dependency-free http module.
Installation
npm install roi -S
Usage
const roi = require('roi');
const options = {endpoint: 'http://localhost:3000/posts'};
roi.get(options)
.then(response => {
console.log(response);
console.log(response.statusCode);
console.log(response.headers);
console.log(response.body);
})
.catch(e => console.log(e));
More examples
POST
const options = {endpoint: 'http://localhost:3000/posts'};
const foo = {
title: 'foo-json',
author: 'bgold'
};
roi.post(options, foo)
.then(response => console.log(response)
.catch(e => console.log(e));
PUT
const options = {endpoint: 'http://localhost:3000/posts/2'};
const foo = {
title: 'foo-json2',
author: 'bgold'
};
roi.put(options, foo)
.then(response => console.log(response))
.catch(e => console.log(e));
DELETE
const options = {endpoint: 'http://localhost:3000/posts/3'};
roi.del(options)
.then(response => console.log(response))
.catch(e => console.log(e));
HEAD
const options = {endpoint: 'http://localhost:3000/posts/3'};
roi.head(options)
.then(response => console.log(response.statusCode === 200))
.catch(e => console.log(e));
DOWNLOAD
const options = {endpoint: 'https://github.com/bucharest-gold/roi/raw/master/test/green.png'};
roi.download(options, '/tmp/green.png')
.then(x => console.log(x))
.catch(e => console.log(e));
UPLOAD
const up = (request, response) => {
request
.pipe(fs.createWriteStream('/tmp/myFileUploaded.png'))
.on('finish', () => {
response.end(request.headers.filename);
});
};
const server = require('http').createServer(up);
server.listen(3002, () => {});
const options = {endpoint: 'http://localhost:3002/'};
roi.upload(options, '/tmp/myFile.png')
.then(response => {
console.log(fs.existsSync('/tmp/myFileUploaded.png'));
});
Basic authentication
const options = {
endpoint: 'http://localhost:3000/',
username: 'admin',
password: 'admin'
};
roi.get(options)
.then(response => console.log(response))
.catch(e => console.log(e));
Contributing
Please read the contributing guide